/*
 *    Copyright (c) 2018-2025, proj All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the proj developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: proj
 */

package com.ykht.proj.coupon.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ykht.proj.common.core.util.R;
import com.ykht.proj.common.log.annotation.SysLog;
import com.ykht.proj.common.security.util.SecurityUtils;
import com.ykht.proj.coupon.entity.FxConfRewardBaseGlobel;
import com.ykht.proj.coupon.service.FxConfRewardBaseGlobelService;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;


/**
 * 平台基本配置
 *
 * @author proj code generator
 * @date 2021-02-01 19:00:18
 */
@RestController
@AllArgsConstructor
@RequestMapping("/fxconfrewardbaseglobel")
@Api(value = "fxconfrewardbaseglobel", tags = "平台基本配置管理")
public class FxConfRewardBaseGlobelController {

	private final FxConfRewardBaseGlobelService fxConfRewardBaseGlobelService;

	/**
	 * 查询全部
	 */
	@ApiOperation(value = "查询c端分销‘赚’字的显隐0显示1隐藏", notes = "c端分销‘赚’字的显隐0显示1隐藏")
	@GetMapping("/queryFxDispaly")
	public R queryFxDispaly() {
		FxConfRewardBaseGlobel one = fxConfRewardBaseGlobelService.list().get(0);
		return R.ok(one.getFxFlagDisplay()==null|| one.getFxFlagDisplay()==1?1:0);
	}
	@ApiOperation(value = "更新c端分销‘赚’字的显隐0显示1隐藏", notes = "c端分销‘赚’字的显隐0显示1隐藏")
	@GetMapping("/updateFxDispaly")
	public R updateFxDispaly(Integer fxFlagDisplay) {
		if (fxFlagDisplay==null) {
			return R.failed("参数为空");
		}
		fxConfRewardBaseGlobelService.update(Wrappers.lambdaUpdate(FxConfRewardBaseGlobel.class).set(fxFlagDisplay!=null,FxConfRewardBaseGlobel::getFxFlagDisplay,fxFlagDisplay));
		return R.ok();
	}
	@ApiOperation(value = "查询平台基本配置", notes = "查询平台基本配置")
	@GetMapping("/query")
//	@PreAuthorize("@pms.hasPermission('coupon_fxconfrewardbaseglobel_view')" )
	public R query() {
		return R.ok(fxConfRewardBaseGlobelService.getOne(Wrappers.lambdaQuery(FxConfRewardBaseGlobel.class)
				.eq(FxConfRewardBaseGlobel::getDelFlag, 0)
				.eq(FxConfRewardBaseGlobel::getSwitchs, 0)
		));
	}

	/**
	 * 查询全部
	 */
	@ApiOperation(value = "查询全部", notes = "查询全部")
	@GetMapping("/findAll")
//	@PreAuthorize("@pms.hasPermission('coupon_fxconfrewardbaseglobel_view')" )
	public R getFxConfRewardBaseGlobel() {
		Map<String, Object> map = new HashMap<>();
		map.put("del_flag", 0);
		List<FxConfRewardBaseGlobel> list = fxConfRewardBaseGlobelService.listByMap(map);
		return R.ok(list);
	}

	@ApiOperation(value = "查询全部", notes = "查询全部")
	@GetMapping("/page")
	public R page() {
		FxConfRewardBaseGlobel one = fxConfRewardBaseGlobelService.getOne(Wrappers.lambdaQuery(FxConfRewardBaseGlobel.class)
				.eq(FxConfRewardBaseGlobel::getDelFlag, 0)
				.eq(FxConfRewardBaseGlobel::getTenantId, 1)
		);
		return R.ok(one);
	}


	/**
	 * 通过id查询平台基本配置
	 *
	 * @param id id
	 * @return R
	 */
	@ApiOperation(value = "通过id查询", notes = "通过id查询")
	@GetMapping("/{id}")
	@PreAuthorize("@pms.hasPermission('coupon_fxconfrewardbaseglobel_view')")
	public R getById(@PathVariable("id") Long id) {
		return R.ok(fxConfRewardBaseGlobelService.getById(id));
	}

	/**
	 * 新增或修改平台基本配置
	 *
	 * @param fxConfRewardBaseGlobel 平台基本配置
	 * @return R
	 */
	@ApiOperation(value = "新增或修改平台基本配置", notes = "新增或修改平台基本配置")
	@SysLog("新增或修改平台基本配置")
	@PostMapping
	public R save(@RequestBody FxConfRewardBaseGlobel fxConfRewardBaseGlobel) {
		if (null == fxConfRewardBaseGlobel.getId() || "".equals(fxConfRewardBaseGlobel.getId())) {
			fxConfRewardBaseGlobel.setCreateUser(SecurityUtils.getUserId());
			fxConfRewardBaseGlobel.setCreateTime(LocalDateTime.now());
		}
		fxConfRewardBaseGlobel.setUpdateUser(SecurityUtils.getUserId());
		fxConfRewardBaseGlobel.setUpdateTime(LocalDateTime.now());
		return R.ok(fxConfRewardBaseGlobelService.saveOrUpdate(fxConfRewardBaseGlobel));
	}

	/**
	 * 保存群组设置
	 * @param
	 * @param
	 * @return
	 */
	@ApiOperation(value = "群组设置", notes = "群组设置")
	@SysLog("群组设置")
	@PostMapping("/saveGroupConf")
	public R saveGroupConf(@RequestBody Map<String,String> map) {
		String qrCodeUrl = map.get("qrCodeUrl");
		String groupName = map.get("groupName");
		if(qrCodeUrl==null){
			return R.failed("群组图片地址不能为空");
		}
		if(groupName==null){
			return R.failed("群组名称不能为空");
		}
		List<FxConfRewardBaseGlobel> fxConfRewardBaseGlobelList = fxConfRewardBaseGlobelService.list();
		Optional<FxConfRewardBaseGlobel> optional = fxConfRewardBaseGlobelList.stream().
				filter(item -> groupName.equals(item.getGroupName())).findFirst();
		if(optional.isPresent()) {
			return R.failed("群组名称已存在");
		}
		FxConfRewardBaseGlobel fxConfRewardBaseGlobel = new FxConfRewardBaseGlobel();
		fxConfRewardBaseGlobel.setUpdateUser(SecurityUtils.getUserId());
		fxConfRewardBaseGlobel.setCreateUser(SecurityUtils.getUserId());
		fxConfRewardBaseGlobel.setCreateTime(LocalDateTime.now());
		fxConfRewardBaseGlobel.setUpdateTime(LocalDateTime.now());
		fxConfRewardBaseGlobel.setGroupName(groupName);
		fxConfRewardBaseGlobel.setQrCodeUrl(qrCodeUrl);
		fxConfRewardBaseGlobelService.save(fxConfRewardBaseGlobel);
  		return R.ok();
	}


	/**
	 * 查询群组设置
	 */
	@ApiOperation(value="查询群组设置",notes="查询群组设置")
	@GetMapping("/queryGroupConf")
	public R queryGroupConf() {
		List<FxConfRewardBaseGlobel> one = fxConfRewardBaseGlobelService.list();
 		return R.ok(one.stream().filter(item -> item.getGroupName() != null).findFirst().get());
	}


}
